import requests
from lxml import etree

url = 'http://www.netbian.com/mei/'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Cookie': 'cf_clearance=HFTsvudtGll2o62bE__jqNVe99vvp0pkZGJ0D8rxUDM-1744797022-1.2.1.1-tEksqoptUkksDJGR6k88gfjwzy9w1EYyTxDVWR401MUXVKXNc0XAf37nPOg2PESd5vSM_3s9i70zRx5GdOfusNCXza1BuEBWq8d7Bu_Gd5oMS9lRvCZY7ZJqUDs6OTo2qodV4R5Pjv4aRXXY66N.FajdGCyKl1RhKV1cxTZ70y9AtEAAEbg1gIX8ROKoJfE2d.5kBdY9eMeoHBG5pY4aUF12_j6iuuVku2gWfn0PJK7kjnd06eOQDre6.DqhCFQXVhVsfTxk_3D63aSWfbDWZPOMt2lgZlLHi5L4XpuwlaRggODC2joRQwxsJeRdgIX6N1vOUgcuHDiGrHJfMr0wBpgdKb9I71WTDFuA9z3YHi2._EMp37XDiwGWI9CuE.Yo',
    'Host': 'www.netbian.com',
    'Referer': 'http://www.netbian.com/mei/?__cf_chl_tk=pQFvfYVZOYEZ2K6FB2PyKgvA7jFHPOCq_RDhe1wD0SM-1744793773-1.0.1.1-BN2pmGc.YvaSpH4r0yewkKENa7.Y5ClqI.yBTY5zp7s',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
}
resp = requests.get(url, headers=headers)
resp.encoding = 'gbk'
print(resp.text)
xp = etree.HTML(resp.text)
img_urls = xp.xpath('//ul/li/a/img/@src')
img_names = xp.xpath('//ul/li/a/img/@alt')
# 保存文件
for u, n in zip(img_urls, img_names):
    print(f'正在下载图片：{n}')
    img_resp = requests.get(u, headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
    })
    with open(f'./图片合成/img/{n}.jpg', 'wb') as f:
        f.write(img_resp.content)
